package geppetto.phraseScorer;

import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsSet;

public  class WeightedPhraseScorerPhraseScorer extends AbstractPhraseScorer{

	AlignmentsSet alignments;
	public WeightedPhraseScorerPhraseScorer(AlignmentsSet alignments) {
		this.alignments = alignments;
	}
	
	public double getPhraseScore(int sentenceNumber,byte sentenceSource,
			int[] foreingSentence, int[] sourceSentence,
			int sourceStartPos, int sourceEndPos,
			int foreignStartPos, int foreignEndPos){
		
		double weight = 0;
		double alpha = 1;
		double beta = 1;
		Alignment al = alignments.get(sentenceNumber);
		for(int sourcePos = 0; sourcePos < al.getSourceLenght(); sourcePos++){
			for(int targetPos = 0; targetPos < al.getForeignLenght(); targetPos++){
				boolean sourceAligned = sourcePos >= sourceStartPos && sourcePos <= sourceEndPos;
				boolean targetAligned = targetPos >= foreignStartPos && targetPos <= foreignEndPos;
				if(sourceAligned && targetAligned){
					alpha *= 1 - al._posteriors[sourcePos][targetPos];
				}
				else if(sourceAligned || targetAligned){
					beta *= 1 - al._posteriors[sourcePos][targetPos];
				}
			}
		}
		return (1 - alpha) * beta;
		/*for(int sourcePos = sourceStartPos; sourcePos <= sourceEndPos; sourcePos++){
			for(int targetPos = foreignStartPos; targetPos <= foreignEndPos; targetPos++){
				if(al.hasPosition(sourcePos, targetPos)){
					weight += al._posteriors[sourcePos][targetPos];
					counts++;					
				}
			}
		}
		return weight / counts;*/
	}
	
//	public abstract double getNullPhraseScore(int sentenceNumber, byte sentenceSource, 
//			int[] foreingSentence, int[] sourceSentence,
//			int foreignStartPos, int foreignEndPos);
	
}
